package org.csu.sei.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.csu.sei.common.BaseConstant;
import org.csu.sei.entity.DTO.FrontTeacherInfoItemDTO;
import org.csu.sei.entity.TeacherInfo;
import org.csu.sei.entity.User;
import org.csu.sei.persistence.TeacherInfoMapper;
import org.csu.sei.persistence.UserMapper;
import org.csu.sei.service.ITeacherInfoService;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import static org.csu.sei.common.BaseConstant.JobTitle.*;

import static org.csu.sei.common.BaseConstant.Role.*;

@Component(value = "teacherInfoService")
public class TeacherInfoServiceImpl extends ServiceImpl<TeacherInfoMapper,TeacherInfo> implements ITeacherInfoService {
    @Resource
    private TeacherInfoMapper teacherInfoMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public int insertOrUpdateTeacherInfo(TeacherInfo teacherInfo) {
        //拆入之前需要进行判断是否存在
        User user = userMapper.selectById(teacherInfo.getId());
        if( user==null ){
            return -1;
        }
        else if(user.getRole()!=TEACHER.getCode()){
            return -2;
        }
        //教师信息在teacherinfo中不存在，则新建插入
        if(teacherInfoMapper.selectById(teacherInfo.getId())==null)
        {
            teacherInfo.setCreateTime(LocalDateTime.now());
            teacherInfo.setUpdateTime(LocalDateTime.now());
            return teacherInfoMapper.insert(teacherInfo);
        }
        //教师信息在teacherInfo中已经存在，则进行更新
        else
        {
            TeacherInfo primary = teacherInfoMapper.selectById(teacherInfo.getId());
            teacherInfo.setCreateTime(primary.getCreateTime());
            teacherInfo.setUpdateTime(LocalDateTime.now());
            return teacherInfoMapper.updateById(teacherInfo);
        }
    }

    @Override
    public int deleteTeacherInfo(Long id) {
        if(teacherInfoMapper.selectById(id)==null){
            return -1;
        }
        return teacherInfoMapper.deleteById(id);
    }

    @Override
    public int updateTeacherInfo(TeacherInfo teacherInfo) {
        if(teacherInfoMapper.selectById(teacherInfo.getId())==null){
            return -1;
        }
        TeacherInfo primary = teacherInfoMapper.selectById(teacherInfo.getId());
        teacherInfo.setCreateTime(primary.getCreateTime());
        teacherInfo.setUpdateTime(LocalDateTime.now());
        return teacherInfoMapper.updateById(teacherInfo);
    }

    @Override
    public TeacherInfo queryTeacherInfo(Long id) {
        return teacherInfoMapper.selectById(id);
    }

    @Override
    public JSONArray TeacherInfoList(){
        JSONArray jsonArray = new JSONArray();
        for (BaseConstant.JobTitle jobTitle: BaseConstant.JobTitle.values()
             ) {
            JSONObject jsonObject = new JSONObject();
            List<TeacherInfo>  teacherInfos =teacherInfoMapper.selectList(new QueryWrapper<TeacherInfo>().eq("job_title",jobTitle.getMsg()));
            List<FrontTeacherInfoItemDTO> teacherInfoItemDTOS = new ArrayList<>();
            for (TeacherInfo teacherInfo : teacherInfos) {
                teacherInfoItemDTOS.add(FrontTeacherInfoItemDTO.returnFrontTeacherInfoItem(teacherInfo));
            }
            jsonObject.put("jobTitle",jobTitle.getMsg());
            jsonObject.put("teacherList",teacherInfoItemDTOS);
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @Override
    public int deleteSome(List<Long> idList) {
        int count = 0;
        for (Long id : idList){
            count = count + deleteTeacherInfo(id);
        }
        return count;
    }


}
